<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="任务管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="800px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
            <el-form-item label="任务名称" prop="taskName">
                <el-input v-model="formData.taskName"/>
            </el-form-item>

            <el-form-item label="消息模版" required>
                <div style="height:500px; overflow-y:scroll;">
                    <div v-for="(item,index) in templateList" style="margin-bottom: 20px;">
                        <span style="margin-right:20px; font-weight: bold; color:#1890FF; font-size: 16px;">{{
                                item.name
                            }}</span>
                        <div style="line-height: 26px;">
                            <el-checkbox
                                v-for="template in item.templateList"
                                :label="template.id"
                                v-model="template.checked">
                                {{ template.name }}
                            </el-checkbox>
                        </div>
                    </div>
                </div>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {MessageTypeApi} from '@/api/message/MessageType'
import {TemplateApi} from "@/api/message/TemplateApi";
import {MessageTaskApi} from "@/api/message/MessageTaskApi";
import {Common} from '@/utils/common'


export default {
    name: 'messageTaskUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: 0,
                taskName: '',
                templateIds: [],
            },
            rules: {
                taskName: [
                    {required: true, message: '请填写任务名称', trigger: 'blur'}
                ]
            },
            dialogVisible: false,

            templateList: []
        }
    },
    created() {

    },
    methods: {
        // 显示组件
        async showUpdate(data) {
            const that = this

            this.dialogVisible = true

            if (this.$refs['formRef'] !== undefined) {
                this.$refs['formRef'].resetFields()
            }

            this.$nextTick(() => {
                if (data.id > 0) {
                    that.formData = Common.assign(this.formData, data)
                } else {
                    this.formData.taskName = ''
                    this.formData.templateIds = []
                }

                TemplateApi.getEnableList('').then((response) => {
                    response.forEach(item => {
                        item.templateList.forEach(template => {
                            template.checked = that.formData.templateIds.indexOf(template.id) > -1
                        })
                    })

                    that.templateList = response
                })
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    const submitData = Object.assign({}, that.formData)
                    submitData.templateIds=[]

                    that.templateList.forEach(serviceProvider => {
                        serviceProvider.templateList.forEach(template => {
                            if (template.checked) {
                                submitData.templateIds.push(template.id)
                            }
                        })
                    })

                    if (submitData.id > 0) {
                        MessageTaskApi.edit(submitData).then(() => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        MessageTaskApi.create(submitData).then(() => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }
    }
}
</script>

<style>
</style>
